﻿namespace Snipps.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Shapes;

    [TemplatePart(Name = PART_Ellipse, Type = typeof(Ellipse))]
    public class CircuralIndicator : Control
    {
        #region Constants and Fields

        private const string PART_Ellipse = "PART_Ellipse";

        public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
            "Brush", typeof(Brush), typeof(CircuralIndicator), new PropertyMetadata(default(Brush)));

        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(
            "Color", typeof(Color), typeof(CircuralIndicator), new PropertyMetadata(default(Color)));

        #endregion

        #region Constructors and Destructors

        static CircuralIndicator()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(CircuralIndicator), new FrameworkPropertyMetadata(typeof(CircuralIndicator)));
        }

        #endregion

        #region Public Properties

        public Brush Brush
        {
            get
            {
                return (Brush)GetValue(BrushProperty);
            }

            set
            {
                SetValue(BrushProperty, value);
            }
        }

        public Color Color
        {
            get
            {
                return (Color)GetValue(ColorProperty);
            }

            set
            {
                SetValue(ColorProperty, value);
            }
        }

        #endregion

        #region Public Methods and Operators

        public override void OnApplyTemplate()
        {
            var e = GetTemplateChild(PART_Ellipse) as Ellipse;
            if (e != null)
            {
                e.Stroke = GetEllipseStroke();
            }

            base.OnApplyTemplate();
        }

        #endregion

        #region Methods

        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            var sizeValue = Math.Min(arrangeBounds.Width, arrangeBounds.Height);
            var e = GetTemplateChild(PART_Ellipse) as Ellipse;
            if (e != null)
            {
                e.StrokeThickness = sizeValue / 7;
            }

            return base.ArrangeOverride(new Size(sizeValue, sizeValue));
        }

        protected override Size MeasureOverride(Size constraint)
        {
            var desiredSize = base.MeasureOverride(constraint);

            var sizeValue = Math.Min(desiredSize.Width, desiredSize.Height);
            desiredSize.Width = sizeValue;
            desiredSize.Height = sizeValue;
            var e = GetTemplateChild(PART_Ellipse) as Ellipse;
            {
                if (e != null)
                {
                    e.StrokeThickness = sizeValue / 7;
                }
            }

            return desiredSize;
        }

        private Brush GetEllipseStroke()
        {
            if (Equals(Brush,default(Brush)))
            {
                var myLinearGradientBrush = new LinearGradientBrush
                                                {
                                                    StartPoint = new Point(0.5, 0), 
                                                    EndPoint = new Point(0.5, 1)
                                                };

                var secondColor = Color.FromArgb(0, Color.R, Color.G, Color.B);
                myLinearGradientBrush.GradientStops.Add(new GradientStop(secondColor, 0.727));
                myLinearGradientBrush.GradientStops.Add(new GradientStop(Color, 1));

                Brush = myLinearGradientBrush;
            }

            return Brush;
        }

        #endregion
    }
}